﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Media;

namespace ResourceEditor.Core.ResourceDataObjects
{
    public class ResourceDataAudio : ResourceData<UnmanagedMemoryStream>, ISupportFileResource, ISupportExecute
    {
        #region Properties
        private SoundPlayer Player { get; set; }
        #endregion

        #region Ctor
        public ResourceDataAudio(ResourceFile container)
            : base(container)
        {

        }
        #endregion

        #region IFileObject Members

        public void Add(string fileName)
        {
            try
            {
                FileInfo info = new FileInfo(fileName);
                using (FileStream reader = new FileStream(fileName, FileMode.Open))
                {
                    byte[] buffer = new byte[reader.Length];
                    reader.Read(buffer, 0, buffer.Length);

                    unsafe
                    {
                        Add(info.Name, GetPinnedBufferMemoryStream(buffer));
                    }
                }
            }
            catch(Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        public void ChangeItem(string key, string fileName)
        {
            try
            {
                FileInfo info = new FileInfo(fileName);
                using (FileStream reader = new FileStream(fileName, FileMode.Open))
                {
                    byte[] buffer = new byte[reader.Length];
                    reader.Read(buffer, 0, buffer.Length);

                    unsafe
                    {
                        ChangeItem(key, GetPinnedBufferMemoryStream(buffer));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        public string GetExtension(string key)
        {
            return ".wav";
        }

        public void Export(string key, string fileName)
        {
            try
            {
                using (FileStream fs = File.OpenWrite(fileName))
                {
                    UnmanagedMemoryStream stream = this[key].Current;
                    
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, buffer.Length);
                    fs.Write(buffer, 0, buffer.Length);
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        #endregion

        #region ISupportExecute Members

        public void Execute(string key)
        {
            try
            {
                if (Player != null)
                {
                    Player.Stop();
                }

                this[key].Current.Position = 0;
                Player = new SoundPlayer(this[key].Current);
                Player.Play();
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        public void Exit(string key)
        {
            try
            {
                if (Player != null)
                {
                    Player.Stop();
                    this[key].Current.Position = 0;
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                throw;
            }
        }

        #endregion

        #region Methods
        private UnmanagedMemoryStream GetPinnedBufferMemoryStream(byte[] buffer)
        {
            Module module = typeof(UnmanagedMemoryStream).Module;
            Type pinnedBufferMemoryStreamType = module.GetTypes().Single(item => item.Name == "PinnedBufferMemoryStream");
            ConstructorInfo ctor = pinnedBufferMemoryStreamType.GetConstructor(
                                                            BindingFlags.NonPublic | BindingFlags.Instance,
                                                            null,
                                                            new Type[] { typeof(byte[]) },
                                                            null);

            UnmanagedMemoryStream pinnedBufferMemoryStream = (UnmanagedMemoryStream)ctor.Invoke(new object[] { buffer });
            return pinnedBufferMemoryStream;
        }
        #endregion
    }
}
